#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>

#define _FIFO_FILE_ "fifo"
#define _DEST_FILE_ "dest.txt"

int main()
{
    //创建管道文件
    int mf = mkfifo(_FIFO_FILE_, 0666);
    if(mf < 0)  exit(1);
    //以读方式打开管道文件
    int rf = open(_FIFO_FILE_, O_RDONLY);
    if(rf < 0)   exit(2);
    //以写方式打开dest.txt文件
    int wd = open(_DEST_FILE_, O_WRONLY | O_TRUNC);
    if(wd < 0)  exit(3);
    //从管道文件中读取
    char buff[1024];
    int n = 0;
    while(n = read(rf, buff,sizeof(buff)-1))
    {
        buff[n] = 0;
        //将读取到的内容写入到dest.txt文件中
        write(wd, buff,strlen(buff));
    }
    printf("read fifo and write dest.txt success\n");
    return 0;
}

